package com.yilin.platform.base_core.application;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

public class AppUtil {
    public static String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    //获取本地地址
    public static String getLocalPath() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) return "http://127.0.0.1:8089";
        HttpServletRequest request = attributes.getRequest();
        return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
    }

    public static String getIpAddressByUrl(String reqUrl) {
        int index1 = reqUrl.indexOf("//") + 2;
        String[] s = reqUrl.split("//");
        int index2 = s[0].length() + 2 + s[1].indexOf(":");
        return reqUrl.substring(index1, index2);
    }
}
